#include "cardbox.h"

CardBox::CardBox()
{
	srand( ( unsigned )time( NULL ) ) ;
	for( int i = 0 ; i < 52 ; ++i )
		this->v_box.push_back( Card().Set( i % 13 + 1 , i / 13 )  ) ;
}

bool CardBox::Empty() const 
{
	return this->v_box.empty() ;
}

unsigned int  CardBox::Amount() 
{
	unsigned int i = 0 ;
	Card card;
	for( i ; i < this->v_box.size() ; ++i )
		if( this->v_box.at( i ) == card )
			i = i - 1 ;
	return i ;
}

void CardBox::Push( Card& obj )
{
	this->v_box.push_back( obj ) ;
	this->v_box.at(  rand() % this->Amount() ).Swap( this->v_box.back() );
}

bool CardBox::Send( Card& obj )
{
	if( this->Amount() > 0 )
		return	this->Remove( obj = this->v_box.at( rand() % this->Amount() ) ) ;
	return false ;
}

bool CardBox::Pick( const unsigned int& pos , Card& obj )
{
	if( pos >= 0 && pos < this->Amount() )
	{
		obj = this->v_box.at( pos ) ;
		this->Remove( obj ) ;
		return true ;
	}
	return false ;
}

bool CardBox::Remove( Card& obj )
{
	for( vector<Card>::iterator Iterator = v_box.begin() ; Iterator != v_box.end(); ++Iterator) 
	{ 
		if( *Iterator == obj ) 
		{ 
			v_box.erase( Iterator ) ;
			return true ;
		} 
	}
	return false ;
}

bool CardBox::Remove( const unsigned int& pos )
{
	if( pos >= 0 && pos < this->v_box.size() )
	for( vector<Card>::iterator Iterator = v_box.begin() ; Iterator != v_box.end(); ++Iterator) 
	{ 
		if( *Iterator == v_box.at( pos ) ) 
		{ 
			v_box.erase( Iterator ) ;
			return true ;
		} 
	}
	return false ;
}

Card& CardBox::Get( const unsigned int & pos ) 
{
	if( pos >= 0 && pos < this->Amount() )
		return this->v_box.at( pos );
	else
		return Card();
}

void CardBox::Reset()
{
	this->v_box.clear() ;
	Card *_ = new Card() ;
	for( int i = 0 ; i < 52 ; ++i )
		this->v_box.push_back( (*_).Set( i % 13 + 1 , i / 13 ) ) ;
	delete _ ;
	_ = NULL ;
}